<?php
/**
 * Created by PhpStorm.
 * User: 38339
 * Date: 2021-05-26
 * Time: 14:58
 */

namespace app\common\business\mysql;
use app\common\model\mysql\CommonMenu as models;

class CommonMenu
{

    /*
     * 获取单条数据
     */
    public static function getOne($where){
        return models::getOne($where);
    }

    /*
     * 获取数据
     */
   public static function getList($where,$fields,$limit_start,$page_size){
       return models::getList($where,$fields,$limit_start,$page_size);
   }

   /*
    * 获取数据条数
    */
   public static function getCount($where){
       return getAllPage(models::getCount($where));
   }

   /*
    * 新增数据
    */
   public static function insert($receiveData,$admin){
       $data = [
           'title' => $receiveData['title'],
           'path' => empty($receiveData['path']) ? '' : $receiveData['path'],
           'status' => $receiveData['status'] == 1 ? 1 : 2,
           'parent_id' => empty($receiveData['parent_id']) ? 0 : $receiveData['parent_id'],
           'order_num' => $receiveData['order_num'] > 0 ?: 0,
           'create_time' => date('Y-m-d H:i:s')
       ];
       models::insert($data);
       //写入日志
       CommonLog::insertLog($admin['id'],'/commonMenu/insert','',$data,1);
       return true;
   }

   /*
    * 修改数据
    */
   public static function edit($receiveData,$admin){
       $data = [
           'title' => $receiveData['title'],
           'path' => empty($receiveData['path']) ? '' : $receiveData['path'],
           'status' => $receiveData['status'] == 1 ? 1 : 2,
           'parent_id' => empty($receiveData['parent_id']) ? 0 : $receiveData['parent_id'],
           'order_num' => $receiveData['order_num'] > 0 ?: 0
       ];
       models::where(['id'=>$receiveData['id']])->update($data);
       //写入日志
       CommonLog::insertLog($admin['id'],'/commonMenu/edit','',$data,2);
       return true;
   }

   /*
    * 更改数据状态
    */
   public static function update($receiveData){

   }

    /*
     * 删除数据
     */
    public static function delete($receiveData,$admin,$load){
        models::where(['id'=>$receiveData['id']])->delete();
        //写入日志
        CommonLog::insertLog($admin['id'],'/commonMenu/delete',$load,'',3);
        return true;
    }

    /*
     * 加载管理员权限
     */
    public static function vipMenu()
    {
        //查询可访问的菜单
        $list = models::where(['parent_id' => 0, 'status' => 1,'is_load'=>1])->order('order_num desc')->paginate()->each(function ($item, $key) {
            $item->children = models::where(['parent_id' => $item['id'], 'status' => 1,'is_load'=>1])->select();
        });
        return $list;
    }

    /*
     * 加载管理员权限
     */
    public static function selectMenu($role_id)
    {
        //查询可访问的菜单
        $menu = CommonAuth::where(['role_id'=>$role_id])->find();
        Log::write('权限数据:'.json_encode($menu));
        if(empty($menu)){
            return [];
        }
        $list = models::where(['parent_id' => 0, 'status' => 1,'is_load'=>1])->whereIn('id', $menu['menu_select_ids'])->order('order_num desc')->paginate()->each(function ($item, $key) use ($menu) {
            $item->children = models::where(['parent_id' => $item['id'], 'status' => 1,'is_load'=>1])->whereIn('id', $menu['menu_select_ids'])->order('order_num desc')->select();
        });
        return $list;
    }

}